include( "shared.lua" )

SelPLY = ""
SelID = ""
SelSID = ""
SelTime = nil
SelPLAYER = nil
SelRANK = ""
EnteredAmount = 10
Command = "No Command"
REASON = "No Reason Specified"
TMen = nil
CD = 15
OptionFrame = nil

//Change this to a TAB system, one tab for each level of commands { Owner, Admin, Moderator }
//Possibly one for player options and commands.

//Actually, first let's try separating them into collapisble categories encompassing the bottom of the admin menu. (FIRST TO DO)
//Devise a more dynamic way of grabing the commands for each category.
//Change the Flags system to a checkbox system.

//Might have to go back and set the concommands to use the same argu ments somehow.

//Fix the checks for Admin / Mod commands (AKA Change Kick to Mod and Points and Time to Admin) ALSO MAKE THE MENU ONLY AVAILABLE FOR M AND UP

//Make F2 close the shop when it is open and do the same for F3  LATER, IT MADE ME ANGRY

PLYER = FindMetaTable( "Player" )

function PLYER:UID()
	return self:GetNetworkedString( "UID" )
end

function PLYER:FLAGS()
	return self:GetNetworkedString( "flags" )
end

function PLYER:TIME( Target )
	RunConsoleCommand( "xqam_SendSingleTime", Target )
end

function PLYER:POINTS()
	return self:GetNetworkedInt( "points" )
end

PLYER = nil



function ReceiveTime_Hook( um ) //Might have to add this stuff to a Think()
	SelTime = um:ReadLong()
	SelPLAYER = um:ReadEntity()
	
	ShowPlayerStats( TMen )
end
usermessage.Hook( "STMSG", ReceiveTime_Hook )

function TMenu()
	if !string.find(LocalPlayer():FLAGS(), "M") then return false end
		TMen = vgui.Create( "DFrame" )
		
			//TMen:SetPos( 50,10 )
			TMen:SetSize( 500,600 )
			TMen:SetDraggable( true )
			TMen:SetSizable( true )
			TMen:SetTitle( "Player List" )
			TMen:ShowCloseButton( true )
			TMen:SetBackgroundBlur( true )
			TMen:SetKeyboardInputEnabled( true )
			TMen:SetVisible( true )
			TMen:Center()
			TMen:MakePopup()
			
		
		local TMenPList = vgui.Create( "DListView" )
		
			TMenPList:SetParent( TMen )
			TMenPList:SetPos(10, 30)
			TMenPList:SetSize(480, 200)
			TMenPList:SetMultiSelect( false )
			TMenPList:AddColumn( "Player" )
			TMenPList:AddColumn( "ID" )
			TMenPList:AddColumn( "Unique ID" )
			
			
			for k,v in pairs(player.GetAll()) do
				TMenPList:AddLine( v:Nick(), v:UserID(), v:UID() )
			end
			TMenPList:SelectFirstItem()
			SelPLY = TMenPList:GetSelected()[1]:GetValue(1)
			SelID = TMenPList:GetSelected()[1]:GetValue(2)
			SelSID = TMenPList:GetSelected()[1]:GetValue(3)	
			RunConsoleCommand( "xqam_SendSingleData", SelSID )

			
			local OCL = TMenPList.OnClickLine;
			TMenPList.OnClickLine = function(parent, line, isselected)
				SelPLY = line:GetValue(1)
				SelID = line:GetValue(2)
				SelSID = line:GetValue(3)
				
				RunConsoleCommand( "xqam_SendSingleData", SelSID )
				
				OCL(parent,line,isselected)
			end
			
			
			ShowPlayerStats( TMen )
			
end
concommand.Add( "ShowADMINMENU", TMenu )

function ShowPlayerStats( parent )

			if SelSID != "" and SelPLAYER and SelPLAYER:IsValid() then
			
				local PStat = vgui.Create( "DPanel", parent ) //This will hold all of our controls for the player.
		
					PStat:SetPos( 5, 235 )
					PStat:SetSize( 490, 360 )
					function PStat:Paint()
						draw.RoundedBox( 4, 0, 0, PStat:GetWide(), PStat:GetTall(), Color( 150,150,150,255 ) )
					end
					function PStat:Think()
						if SelPLAYER and PStat and SelRANK != SelPLAYER:RANK() and string.find( LocalPlayer():FLAGS(), "O" ) then ShowOStats( PStat ) end
					end


		/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		Base player controls here.
		~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
		if string.find(LocalPlayer():FLAGS(), "A") then
		
			local PHLabel = vgui.Create( "DLabel", PStat )
				PHLabel:SetTextColor( color_white )
				PHLabel:SetText( "Player Health:" )
				PHLabel:SizeToContents()
				PHLabel:SetPos( 5, 5 )
		
			local PHealth = vgui.Create( "DTextEntry", PStat )
			local PHLX, PHLY = PHLabel:GetPos()
				PHealth:SetPos( PHLX, PHLY + 20 )
				PHealth:SetSize( 50, 20 )
				PHealth:SetEnterAllowed( true )
				PHealth:SetNumeric( true )
				PHealth:SetValue( SelPLAYER:Health() )
				PHealth.OnEnter = function()
					RunConsoleCommand( "Set_Health", SelSID, PHealth:GetValue() )
				end
				
				
			local PTLabel = vgui.Create( "DLabel", PStat )
				local TH = math.floor(SelTime / 3600)
				PTLabel:SetTextColor( color_white )
				PTLabel:SetText( "Player Time: " .. TH .. ":" .. os.date("%M:%S", SelTime ) )
				PTLabel:SizeToContents()
				PTLabel:SetPos( 5, PHLabel:GetTall() + PHealth:GetTall() + 25 )
					
			local PAddTime = vgui.Create( "DButton", PStat )
			local PTLX,PTLY = PTLabel:GetPos()
				PAddTime:SetPos( PTLX, PTLY + PTLabel:GetTall() + 5 )
				PAddTime:SetText( "Modify Time" )
				PAddTime:SetSize( 75, 20 )
				PAddTime.DoClick = function()
					if SelPLAYER and SelPLAYER:IsValid() then
					local MenuOptions = vgui.Create( "DMenu", PStat )
						MenuOptions:AddOption("Add 30 Minutes", function() RunConsoleCommand( "Add_Hours", SelSID, 1800 ) end )
						MenuOptions:AddOption("Add 1 Hour", function() RunConsoleCommand( "Add_Hours", SelSID, 3600 ) end )
						MenuOptions:AddOption("Add 10 Hours", function() RunConsoleCommand( "Add_Hours", SelSID, 36000 ) end )
						MenuOptions:AddOption("Add 100 Hours", function() RunConsoleCommand( "Add_Hours", SelSID, 360000 ) end )
						MenuOptions:AddSpacer()
						MenuOptions:AddOption("Remove 30 Minutes", function() RunConsoleCommand( "Add_Hours", SelSID, -1800 ) end )
						MenuOptions:AddOption("Remove 1 Hour", function() RunConsoleCommand( "Add_Hours", SelSID, -3600 ) end )
						MenuOptions:AddOption("Remove 10 Hours", function() RunConsoleCommand( "Add_Hours", SelSID, -36000 ) end )
						MenuOptions:AddOption("Remove 100 Hours", function() RunConsoleCommand( "Add_Hours", SelSID, -360000 ) end )
						MenuOptions:Open()
					end
				end
				
				
			local PPLabel = vgui.Create( "DLabel", PStat )
			local PTLBX, PTLBY = PTLabel:GetPos()
				PPLabel:SetTextColor( color_white )
				PPLabel:SetText( "Player Points:" )
				PPLabel:SizeToContents()
				PPLabel:SetPos( 5, PTLBY + PTLabel:GetTall() + PAddTime:GetTall() + 25 )
		
			local PPoints = vgui.Create( "DTextEntry", PStat )
			local PPLX, PPLY = PPLabel:GetPos()
				PPoints:SetPos( PPLX, PPLY + 20 )
				PPoints:SetSize( 50, 20 )
				PPoints:SetEnterAllowed( true )
				PPoints:SetNumeric( true )
				PPoints:SetValue( SelPLAYER:POINTS() )
				PPoints.OnEnter = function()
					if SelPLAYER and SelPLAYER:IsValid() then
						RunConsoleCommand( "Add_Points", SelSID, PPoints:GetValue() - SelPLAYER:POINTS() )
					end
				end
		end
				
				
			local ReasLabel = vgui.Create( "DLabel", PStat )
				ReasLabel:SetTextColor( color_white )
				ReasLabel:SetText( "Kick/Ban Reason:" )
				ReasLabel:SizeToContents()
				ReasLabel:SetPos( 5, 175 )
				
			local ReasonEntry = vgui.Create( "DTextEntry", PStat )
			local RLBX, RLBY = ReasLabel:GetPos()
				ReasonEntry:SetPos( RLBX, RLBY + 20 )
				ReasonEntry:SetSize( 100, 20 )
				ReasonEntry:SetEnterAllowed( true )
				ReasonEntry:SetValue( REASON )
				ReasonEntry.OnEnter = function()
					REASON = ReasonEntry:GetValue()
				end
				
			local KickBut = vgui.Create( "DButton", PStat )
				KickBut:SetPos( RLBX, RLBY + ReasonEntry:GetTall() + 30 )
				KickBut:SetSize( 75, 25 )
				KickBut:SetText( "Kick Player" )
				KickBut.DoClick = function( btn )
					if SelPLAYER and SelPLAYER:IsValid() then
						RunConsoleCommand( "Kick_Player", SelSID, REASON )
						TMen:Remove()
					end
				end
				
				
		if string.find(LocalPlayer():FLAGS(), "A") then
				
			local BanBut = vgui.Create( "DButton", PStat )
				BanBut:SetPos( RLBX, RLBY + ReasonEntry:GetTall() + 65 )
				BanBut:SetSize( 75, 25 )
				BanBut:SetText( "Ban Player" )
				BanBut.DoClick = function( btn )
					if SelPLAYER and SelPLAYER:IsValid() then
					local MenuOptions = vgui.Create( "DMenu", BanBut )
						MenuOptions:AddOption("For 10 Minutes", function() RunConsoleCommand( "Ban_Player", SelSID, 600, REASON, "10 Minutes" ) TMen:Remove() end )
						MenuOptions:AddOption("For 30 Minutes", function() RunConsoleCommand( "Ban_Player", SelSID, 1800, REASON, "30 Minutes" ) TMen:Remove() end )
						MenuOptions:AddOption("For 1 Hour", function() RunConsoleCommand( "Ban_Player", SelSID, 3600, REASON, "1 Hour" ) TMen:Remove() end )
						MenuOptions:AddOption("For 10 Hours", function() RunConsoleCommand( "Ban_Player", SelSID, 36000, REASON, "10 Hours" ) TMen:Remove() end )
						MenuOptions:AddOption("For 1 Day", function() RunConsoleCommand( "Ban_Player", SelSID, 86400, REASON, "1 Day" ) TMen:Remove() end )
						MenuOptions:AddOption("For 1 Week", function() RunConsoleCommand( "Ban_Player", SelSID, 604800, REASON, "1 Week" ) TMen:Remove() end )
						MenuOptions:AddOption("For 1 Month", function() RunConsoleCommand( "Ban_Player", SelSID, 2419200, REASON, "1 Month" ) TMen:Remove() end )
						MenuOptions:Open()
					end
				end
		end
				
				if string.find(LocalPlayer():FLAGS(), "O") then
					ShowOStats( PStat )
				end
				
				AddCommands( PStat )
				
		end
end

function ShowOStats( parent )

	if SelSID != "" then
	
			local FlagsList = vgui.Create( "DPanelList", parent )
				FlagsList:SetPos( 150, 5 )
				FlagsList:SetSize( 100, 150 )
				FlagsList:SetSpacing( 5 )
				FlagsList:EnableHorizontal( false )
				FlagsList:EnableVerticalScrollbar( true )
				
				for k,v in pairs( FlagTable ) do
					local Flag = vgui.Create( "DCheckBoxLabel", FlagsList )
						Flag:SetText( v .. " FLAG" )
						if string.find( SelPLAYER:FLAGS(), v ) then
							Flag:SetValue( 1 )
						else
							Flag:SetValue( 0 )
						end
						Flag:SizeToContents()
						Flag.OnChange = function()
							if SelPLAYER and SelPLAYER:IsValid() then
								if Flag:GetChecked(true) then
									RunConsoleCommand( "Add_Player_Flags", SelSID, v )
								else
									RunConsoleCommand( "Remove_Player_Flags", SelSID, v )
								end
							end
						end
					FlagsList:AddItem( Flag )
				end
			
			
			local RankList = vgui.Create( "DPanelList", parent )
			local FlagLX, FlagLY = FlagsList:GetPos()
				RankList:SetPos( FlagLX, FlagLY + FlagsList:GetTall() + 10 )
				RankList:SetSize( 100, 150 )
				RankList:SetSpacing( 5 )
				RankList:EnableHorizontal( false )
				RankList:EnableVerticalScrollbar( true )
				
				for k,v in pairs( RankTable ) do
					local Rank = vgui.Create( "DCheckBoxLabel", RankList )
						Rank:SetText( v )
						Rank:SizeToContents()
						Rank.OnChange = function()
						if SelPLAYER and SelPLAYER:IsValid() then
							if Rank:GetChecked(true) and SelPLAYER:RANK() != v then
								RunConsoleCommand( "Set_Rank", SelSID, v )
								SelRANK = v
								Rank:SetMouseInputEnabled( false )
									for cn,cb in pairs( RankList:GetItems() ) do
										if cb != Rank then
											cb:SetValue( 0 )
											cb:SetMouseInputEnabled( true )
										end
									end
							end
						end
						end
					RankList:AddItem( Rank )
				end
				for cn,cb in pairs( RankList:GetItems() ) do
					if RankTable[cn] == SelPLAYER:RANK() then
						cb:SetValue( 1 )
						SelRANK = RankTable[cn]
						cb:SetMouseInputEnabled( false )
					else
						cb:SetValue( 0 )
						cb:SetMouseInputEnabled( true )
					end
				end
				
	end
end

function AddCommands( parent )

	if SelSID != "" then
	
		local CommandsList = vgui.Create( "DPanelList", parent )
			CommandsList:SetPos( 280, 5 )
			CommandsList:SetSize( 200, 300 )
			CommandsList:SetSpacing( 2 )
			CommandsList:EnableHorizontal( false )
			CommandsList:EnableVerticalScrollbar( true )
	
	
		if string.find(LocalPlayer():FLAGS(), "M") then
			for k,v in pairs(ModCommands) do
				local but = vgui.Create( "DButton", CommandList )
					but:SetText( string.Replace(v, "_", " ") )
					but:SetSize( 200, 25 )
					but.DoClick = function( btn )
					if SelPLAYER and SelPLAYER:IsValid() then
						if v == "Hurt_Player" then
							local HurtOptions = vgui.Create( "DMenu", but )
								HurtOptions:AddOption("1 Damage", function() RunConsoleCommand( v, SelSID, 1 ) end )
								HurtOptions:AddOption("10 Damage", function() RunConsoleCommand( v, SelSID, 10 ) end )
								HurtOptions:AddOption("50 Damage", function() RunConsoleCommand( v, SelSID, 50 ) end )
								HurtOptions:AddOption("100 Damage", function() RunConsoleCommand( v, SelSID, 100 ) end )
								HurtOptions:AddOption("1000 Damage", function() RunConsoleCommand( v, SelSID, 1000 ) end )
								HurtOptions:Open()
						elseif v == "Toggle_Godmode" then
							local GodOptions = vgui.Create( "DMenu", but )
								GodOptions:AddOption("On", function() RunConsoleCommand( v, SelSID, "1" ) end )
								GodOptions:AddSpacer()
								GodOptions:AddOption("Off", function() RunConsoleCommand( v, SelSID, "0" ) end )
								GodOptions:Open()
						elseif v == "Toggle_Player_NoClip" then
							local NCOptions = vgui.Create( "DMenu", but )
								NCOptions:AddOption("Allow", function() RunConsoleCommand( v, SelSID, "1" ) end )
								NCOptions:AddSpacer()
								NCOptions:AddOption("Disallow", function() RunConsoleCommand( v, SelSID, "0" ) end )
								NCOptions:Open()
						else
							RunConsoleCommand( v, SelSID )
						end
					end
					end
				CommandsList:AddItem( but )
			end
		end
		
		if string.find(LocalPlayer():FLAGS(), "A") then
			for k,v in pairs(AdminCommands) do
				local but = vgui.Create( "DButton", CommandList )
					but:SetText( string.Replace(v, "_", " ") )
					but:SetSize( 200, 25 )
					but.DoClick = function( btn )
					if SelPLAYER and SelPLAYER:IsValid() then
						if v == "Notices_Menu" then	//Leave this if for when I add more Owner Commands
							vgui.Create( "NoticeEditor" )
						elseif v == "ChangeMap_Menu" then
							vgui.Create( "MapMenu" )
						end
					end
					end
				CommandsList:AddItem( but )
			end
		end
		
		if string.find(LocalPlayer():FLAGS(), "O") then
			for k,v in pairs(OwnerCommands) do
				local but = vgui.Create( "DButton", CommandList )
					but:SetText( string.Replace(v, "_", " ") )
					but:SetSize( 200, 25 )
					but.DoClick = function( btn )
					if SelPLAYER and SelPLAYER:IsValid() then
						if v == "Rebuild_Store_Tables" then	//Leave this if for when I add more Owner Commands
							RunConsoleCommand( v, SelSID )
						elseif v == "PP_Limits" then
							vgui.Create( "PPLimits" )
						end
					end
					end
				CommandsList:AddItem( but )
			end
		end
	
	
	end

end


/*~~~~~~~~~~~~~~~~~~~~~~
Text Commands
~~~~~~~~~~~~~~~~~~~~~~*/

function ChatCommands( ply, text, teamchat, alive )
if ply == LocalPlayer() then

		for k,v in pairs( MemberCommands ) do
			//print( v )
			for w in string.gmatch( text, "%!%w+" ) do
				if string.lower(w) == ("!" .. v) then
					if v == "tp" then
						RunConsoleCommand( v, ply:UID() )
					else
						local PLYNAME = string.Replace( text, w .. " ", "" )
						local PUID = ""
					
						for pn,p in pairs( player.GetAll() ) do
							if string.find( p:Nick(), PLYNAME ) then 
								PUID = p:UID()
							end
						end
						if PUID != "" then
							RunConsoleCommand( v, PUID )
						else 
							ply:PrintMessage( HUD_PRINTTALK, "Player not found" )
						end
					end
				end
			end
		end
end
	
end
hook.Add( "OnPlayerChat", "ChatCommands", ChatCommands )

function TeleportRequest( um )
	local Sender = um:ReadEntity()
	local action = um:ReadString()
	
	if OptionFrame and OptionFrame:IsValid() then
		RunConsoleCommand( "HasRequest", Sender:UID() ) return false
	end
		
	OptionFrame = vgui.Create( "DFrame" )
		OptionFrame:SetSize( 275, 120 )
		OptionFrame:AlignLeft( 10 )
		OptionFrame:AlignTop( ScrH() * 0.3 )
		OptionFrame:SetTitle( "Teleport Request" )
		OptionFrame:ShowCloseButton( false )
		function OptionFrame:Paint()
			draw.RoundedBox( 4, 0, 0, 275, 120, Color( 100, 100, 100, 150 ) )
		end
		//OptionFrame:MakePopup()
		OptionFrame:MoveToFront()
		
	local OptionLabel = vgui.Create( "DLabel", OptionFrame )
		OptionLabel:SetText( Sender:Nick() .. " has requested to\n " .. action .. ", will you accept?" )
		OptionLabel:SizeToContents()
		OptionLabel:Center()
		OptionLabel:AlignTop( 30 )
		
	local AutoLabel = vgui.Create( "DLabel", OptionFrame )
		AutoLabel:SetText( "Automatically rejecting in " .. CD .. " seconds" )
		AutoLabel:SizeToContents()
		AutoLabel:Center()
		AutoLabel:AlignBottom( 35 )
		
	local YesB = vgui.Create( "DLabel", OptionFrame )
		YesB:SetText( "PageUp to accept" )
		YesB:SizeToContents()
		YesB:AlignBottom( 5 )
		YesB:AlignLeft( 15 )
		YesB:NoClipping( true )
		function YesB:Paint()
			draw.RoundedBox( 4, -10, -5, YesB:GetWide() + 20, YesB:GetTall() + 10, Color( 80, 80, 80, 150 ) )
			draw.RoundedBox( 4, -5, 0, YesB:GetWide() + 10, YesB:GetTall(), Color( 120, 120, 120, 150 ) )
		end
		
	local NoB = vgui.Create( "DLabel", OptionFrame )
		NoB:SetText( "PageDown to reject" )
		NoB:SizeToContents()
		NoB:AlignBottom( 5 )
		NoB:AlignRight( 15 )
		NoB:NoClipping( true )
		function NoB:Paint()
			draw.RoundedBox( 4, -10, -5, NoB:GetWide() + 20, NoB:GetTall() + 10, Color( 80, 80, 80, 150 ) )
			draw.RoundedBox( 4, -5, 0, NoB:GetWide() + 10, NoB:GetTall(), Color( 120, 120, 120, 150 ) )
		end
		
		function OptionFrame:Think()
			if input.IsKeyDown( 76 ) then
				RunConsoleCommand( "AcceptTeleportRequest", Sender:UID(), action )
				OptionFrame:Remove()
				timer.Destroy( "AutoReject" )
				CD = 15
			elseif input.IsKeyDown( 77 ) then
				RunConsoleCommand( "RejectTeleportRequest", Sender:UID() )
				OptionFrame:Remove()
				timer.Destroy( "AutoReject" )
				CD = 15
			end
			AutoLabel:SetText( "Automatically rejecting in " .. CD .. " seconds" )
		end
		
		timer.Create( "AutoReject", 1, 0, AutoRejectRequest, Sender:UID(), OptionFrame )
		
end
usermessage.Hook( "TRequest", TeleportRequest )

function AutoRejectRequest( UID, OF )
	CD = CD - 1
	if CD <= 0  then 
		RunConsoleCommand( "RejectTeleportRequest", UID )
		OF:Remove()
		timer.Destroy( "AutoReject" )
		CD = 15
	end
end